package com.crl.ssc.ent.dao;

import com.cars.util.page.PageUtil;
import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import org.springframework.stereotype.Repository;
import java.util.List;
import com.crl.ssc.ent.model.EntInfo;
import com.crl.ssc.ent.vo.EntInfoSearchVO;


/**
 * @description: 企业管理-基本信息Dao
 * @author: 张三
 * @version: 1.0
 * @date: 2020-08-07 00:21:01
 */
@Repository
public class EntInfoDao extends BaseDao<EntInfo, EntInfoSearchVO> {
    /**
     * 新增
     *
     * @param entInfo
     * @return
     */
    public int add(EntInfo entInfo) {
        String sql = "INSERT INTO ent_info(ent_code,ent_name,contacts,mobile,emp_num) " +
                "VALUES(:entCode,:entName,:contacts,:mobile,:empNum)";
        return insert(sql, entInfo);
    }

    /**
     * 修改
     *
     * @param entInfo
     * @return
     */
    public int update(EntInfo entInfo) {
        String sql = "UPDATE ent_info SET ent_code=:entCode,ent_name=:entName,contacts=:contacts,mobile=:mobile,emp_num=:empNum " +
                    "WHERE id=:id";
        return update(sql, entInfo);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id) {
        String sql = "DELETE FROM ent_info WHERE id=?";
        return delete(sql, id);
    }
    /**
     * 列表
     *
     * @param entInfoSearchVO
     * @return
     */
    public List<EntInfo> list(EntInfoSearchVO entInfoSearchVO) {
        String sql = "SELECT id,ent_code,ent_name,contacts,mobile,emp_num FROM ent_info WHERE 1=1 ";
        sql += createSearchSQL(entInfoSearchVO);
        sql += " ORDER BY id ASC";
        sql = PageUtil.createMysqlPageSql(sql, entInfoSearchVO.getPage());
        return list(sql, entInfoSearchVO);
    }
    /**
     * 查询数据条数
     *
     * @param entInfoSearchVO
     * @return
     */
    public int count(EntInfoSearchVO entInfoSearchVO) {
        String sql = "SELECT COUNT(id) FROM ent_info WHERE 1=1 ";
        sql += createSearchSQL(entInfoSearchVO);
        return count(sql, entInfoSearchVO);
    }
    /**
       * 拼接查询条件
       *
       * @param entInfoSearchVO
       * @return
    */
    private String createSearchSQL(EntInfoSearchVO entInfoSearchVO) {
        String sql = "";
        if (StringUtil.isNotNullOrEmpty(entInfoSearchVO.getEntCode())) {
			sql += " AND ent_code =:entCode ";
		}if (StringUtil.isNotNullOrEmpty(entInfoSearchVO.getEntName())) {
			sql += " AND ent_name =:entName ";
		}
        return sql;
    }

    /**
     * 获取单个
     *
     * @param id
     * @return
     */
    public EntInfo get(Integer id) {
        String sql = "SELECT id,ent_code,ent_name,contacts,mobile,emp_num FROM ent_info WHERE id=?";
        return get(sql, id);
    }
}
